<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
    <title>ShellCheck: SC2206 – Quote to prevent word splitting/globbing, or split robustly with mapfile or `read -a`.</title>
    <link rel="stylesheet" href="css/bootstrap.min.css" />
  </head>
  <body style="margin-left: auto; margin-right: auto; max-width: 800px">
    <h1>SC2206 – ShellCheck Wiki</h1>
    <a href="https://github.com/koalaman/shellcheck/wiki/SC2206">See this page on GitHub</a>
    <p style="display: none"><a href="index.html">Sitemap</a></p>
    <hr />
    <h2
id="quote-to-prevent-word-splittingglobbing-or-split-robustly-with-mapfile-or-read--a">Quote
to prevent word splitting/globbing, or split robustly with mapfile or
<code>read -a</code>.</h2>
<h3 id="problematic-code">Problematic code:</h3>
<div class="sourceCode" id="cb1"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb1-1"><a href="SC2206.html#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="va">array</span><span class="op">=</span><span class="va">(</span> <span class="va">$var</span> <span class="va">)</span></span></code></pre></div>
<h3 id="correct-code">Correct code:</h3>
<p>If the variable should be a single element:</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb2-1"><a href="SC2206.html#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="va">array</span><span class="op">=</span><span class="va">(</span> <span class="st">&quot;</span><span class="va">$var</span><span class="st">&quot;</span> <span class="va">)</span></span></code></pre></div>
<p>If it's multiple lines, each of which should be an element:</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb3-1"><a href="SC2206.html#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="co"># For bash</span></span>
<span id="cb3-2"><a href="SC2206.html#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="ex">mapfile</span> <span class="at">-t</span> array <span class="op">&lt;&lt;&lt;</span> <span class="st">&quot;</span><span class="va">$var</span><span class="st">&quot;</span></span>
<span id="cb3-3"><a href="SC2206.html#cb3-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-4"><a href="SC2206.html#cb3-4" aria-hidden="true" tabindex="-1"></a><span class="co"># For ksh</span></span>
<span id="cb3-5"><a href="SC2206.html#cb3-5" aria-hidden="true" tabindex="-1"></a><span class="bu">printf</span> <span class="st">&#39;%s\n&#39;</span> <span class="st">&quot;</span><span class="va">$var</span><span class="st">&quot;</span> <span class="kw">|</span> <span class="cf">while</span> <span class="va">IFS</span><span class="op">=</span><span class="st">&quot;&quot;</span> <span class="bu">read</span> <span class="at">-r</span> <span class="va">line</span><span class="kw">;</span> <span class="cf">do</span> <span class="va">array</span><span class="op">+=</span><span class="va">(</span><span class="st">&quot;</span><span class="va">$line</span><span class="st">&quot;</span><span class="va">)</span><span class="kw">;</span> <span class="cf">done</span></span></code></pre></div>
<p>If it's a line with multiple words (separated by spaces, other
delimiters can be chosen with IFS), <a
href="https://stackoverflow.com/a/30212526">each of which should be an
element</a>:</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb4-1"><a href="SC2206.html#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="co"># For bash</span></span>
<span id="cb4-2"><a href="SC2206.html#cb4-2" aria-hidden="true" tabindex="-1"></a><span class="va">IFS</span><span class="op">=</span><span class="st">&quot; &quot;</span> <span class="bu">read</span> <span class="at">-r</span> <span class="at">-a</span> <span class="va">array</span> <span class="op">&lt;&lt;&lt;</span> <span class="st">&quot;</span><span class="va">$var</span><span class="st">&quot;</span></span>
<span id="cb4-3"><a href="SC2206.html#cb4-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-4"><a href="SC2206.html#cb4-4" aria-hidden="true" tabindex="-1"></a><span class="co"># For ksh</span></span>
<span id="cb4-5"><a href="SC2206.html#cb4-5" aria-hidden="true" tabindex="-1"></a><span class="va">IFS</span><span class="op">=</span><span class="st">&quot; &quot;</span> <span class="bu">read</span> <span class="at">-r</span> <span class="at">-A</span> <span class="va">array</span> <span class="op">&lt;&lt;&lt;</span> <span class="st">&quot;</span><span class="va">$var</span><span class="st">&quot;</span></span></code></pre></div>
<h3 id="rationale">Rationale:</h3>
<p>You are expanding a variable unquoted in an array. This will invoke
the shell's sloppy word splitting and glob expansion.</p>
<p>Instead, prefer explicitly splitting (or not splitting):</p>
<ul>
<li>If the variable should become a single array element, quote it.</li>
<li>If you want to split into lines or words, use <code>mapfile</code>,
<code>read -ra</code> and/or <code>while</code> loops as
appropriate.</li>
</ul>
<p>This prevents the shell from doing unwanted splitting and glob
expansion, and therefore avoiding problems with data containing spaces
or special characters.</p>
<h3 id="exceptions">Exceptions:</h3>
<p>If you have already taken care (through setting IFS and
<code>set -f</code>) to have word splitting work the way you intend, you
can <a href="ignore.html">ignore</a> this warning.</p>
    <hr />
    <p style='font-size: 80%'><a href="../index.html">ShellCheck</a> is a static analysis tool for shell scripts. This page is part of its documentation.</p>
  </body>
</html>


